package com.zyj.test.month1.day5;

/**
 * @author Yijia Zhang
 * @create 2024-04-26 23:55
 */
public class code0006 {
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        int loopSize = (numRows << 1) - 2;
        StringBuilder[] sbs = new StringBuilder[numRows];
        for (int i = 0; i < sbs.length; i++) {
            sbs[i] = new StringBuilder(s.length());
        }
        for (int i = 0; i < s.length(); i++) {
            int loopIdx = i % loopSize;
            if (loopIdx < numRows) {
                sbs[loopIdx].append(s.charAt(i));
            } else {
                sbs[2 * numRows - 2 -loopIdx].append(s.charAt(i));
            }
        }
        StringBuilder res = new StringBuilder(s.length());
        for (StringBuilder sb : sbs) {
            res.append(sb);
        }
        return res.toString();
    }
}
